import axios from 'axios';
const serialize = (data = {}) => {
    const dataStr = [];
    Object.keys(data).forEach((key) => {
        const value = data[key];
        if (value !== null && value !== undefined) {
            dataStr.push(`${key}=${encodeURI(value)}`);
        }
    });
    let result = dataStr.join('&');
    if (result.indexOf('+') > -1) {
        result = result.replace('+', '%2B');
    }
    return result;
};
export const post = (url, params, type) => {
    let config = null;
    if (type === 'formdata') {
        config = {
            headers: { 'Content-Type': 'multipart/form-data' },
            withCredentials: true,
        };
    }
    if (type === 'blob') {
        config = {
            responseType: 'blob',
            responseText: 'DOMString',
            withCredentials: true,
        };
    }
    return axios
        .post(url, params, config)
        .then((response) => {
            if (response && response.data) {
                const result = response.data;
                if (result instanceof Blob) {
                    const disposition = response.headers['content-disposition'];
                    return {
                        data: result,
                        etc: disposition.split('=')[1],
                    };
                } else {
                    if (result.state === 0) {
                        return result.data;
                    } else {
                        throw result.data;
                    }
                }
            } else {
                throw response;
            }
        })
        .catch((e) => {
            throw e;
        });
};
